//
//  AddNewCityTableViewController.swift
//  weather
//
//  Created by 陈思斌 on 29/8/17.
//  Copyright © 2017年 陈思斌. All rights reserved.
//

import UIKit

class AddNewCityTableViewController: UITableViewController {
    var allCities = [String]()
    override var prefersStatusBarHidden: Bool{
        return true
    }
    override func viewDidLoad() {
        super.viewDidLoad()
//        if #available(iOS 9.0, *) {
//            UILabel.appearance(whenContainedInInstancesOf:[UITableViewHeaderFooterView.self]).font = UIFont.systemFont(ofSize: 26)
//        } else {
//            // Fallback on earlier versions
//        }
        
        let path = Bundle.main.path(forResource: "default-city", ofType: ".plist")
        self.allCities =  NSArray(contentsOfFile: path!) as! [String]
        
        self.tableView.register(UITableViewCell.self,forCellReuseIdentifier:"reuseIdentifier")
        self.tableView.separatorStyle = .none
        
        self.tableView.backgroundColor = leftControllerAndRightControllerBgColor
        
        initHeaderView()
        
    }
    func initHeaderView(){
        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 44))
        let searchTextField = UITextField(frame: CGRect(x: 20, y: 14, width: self.view.bounds.size.width - 40, height: 30))
        searchTextField.backgroundColor = UIColor.white
        searchTextField.layer.cornerRadius = 15
        searchTextField.layer.masksToBounds = true
        searchTextField.leftView = UIImageView(image: UIImage(named: "search_b"))
        searchTextField.leftViewMode = .always
        searchTextField.placeholder = "城市名称或拼音"
        searchTextField.textColor = UIColor.black
        headerView.addSubview(searchTextField)
        self.tableView.tableHeaderView = headerView
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
      
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return  1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       
        return allCities.count + 1
    }
       override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
        cell.backgroundColor = leftControllerAndRightControllerBgColor
        cell.textLabel?.textColor = UIColor.white
        if indexPath.row == 0 {
            cell.textLabel?.text = "自动定位"
            cell.imageView?.image = UIImage(named: "city")
        }else {
            cell.textLabel?.text = self.allCities[indexPath.row - 1]
            cell.imageView?.image = nil
        }
        return cell
    }
 
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0{
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: AutoLocationNotification), object: nil)
        }else{
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: ChooseLocationCityNotification), object: nil, userInfo: ["choose_city":allCities[indexPath.row - 1]])
        }
        self.dismiss(animated: true, completion: nil)
    }
    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
